package hotciv.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import minidraw.standard.AbstractFigure;

/** A figure representing a text.
 * Rather crude at the moment.
<#if type == "code">

<#include "/data/author.txt">
</#if>
 */
public class TextFigure extends AbstractFigure {
  private Point position;
  private String text;
  private Font fFont;
  FontMetrics metrics = null;

  public TextFigure(String text, Point position) {
    this.position = position;
    this.text = text;
    fFont = new Font("Serif", Font.BOLD, 20);
  }

  public void setText(String newText) {
    willChange();
    text = newText;
    changed();
  }

  @Override
protected void basicMoveBy(int dx, int dy) {
    position.translate(dx, dy);
  }

  public Rectangle displayBox() {
    Dimension extent = textExtent();
    return new Rectangle(position.x, position.y, extent.width, extent.height);
  }

  public void draw(Graphics g) {
    g.setFont(fFont);
    g.setColor( Color.white);
    metrics = g.getFontMetrics(fFont);
    g.drawString(text, position.x, position.y + metrics.getAscent());
  }

  private Dimension textExtent() {
    // metrics may not have been defined yet if no drawing
    // has occurred, however the error is removed upon first
    // redrawing.
    int fWidth;
    int fHeight; 
    if ( metrics == null ) {
      fWidth = 50;
      fHeight = 20;
    } else {
      fWidth = metrics.stringWidth(text);
      fHeight = metrics.getHeight();
    }
    return new Dimension(fWidth, fHeight);
  }
}
